import * as _ from 'lodash';
import * as Immutable from 'seamless-immutable';

import WorkViewComponent from '../component/common/WorkViewComponent';

import Dashboard from '../page/dashboard/Dashboard';
import Customer from '../page/customer/Customer';
import Report from '../page/report/Report';
import Workflow from '../page/workflow/Workflow';

export const workViews = [];
// 工作台
workViews.push({
  id: 1,
  deep: 1,
  pathRegExp: /^$|^\/?#?$|^#$|^#\/$|^#\/dashboard\/?[\s\S]*?$/,
  path: '#/',
  name: 'Dashboard',
  text: '控制台1',
  component: Dashboard,
});
// 顾客列表
workViews.push({
  id: 2,
  deep: 1,
  pathRegExp: /^#\/customer\/?[\s\S]*?$/,
  path: '#/customer',
  name: 'Customer',
  text: '顾客列表',
  component: Customer,
});
// 工作流
workViews.push({
  id: 8,
  deep: 1,
  pathRegExp: /^#\/workflow\/?[\s\S]*?$/,
  path: '#/workflow',
  name: 'Workflow',
  text: '工作流程',
  component: Workflow
});
// 报表
workViews.push({
  id: 9,
  deep: 1,
  pathRegExp: /^#\/report\/?[\s\S]*?$/,
  path: '#/report',
  name: 'Report',
  text: '报表',
  component: Report
});


export interface WorkView extends Immutable.ImmutableObject<WorkView> {
  id: number; //  可以考虑删掉？
  deep: number;
  isActive: boolean;
  pathRegExp: RegExp; //  直接判断path是否相等？
  path: string;
  pathParam: string;
  name: string; //  考虑删掉?
  component: typeof WorkViewComponent;
  text: string;
}


export const getViewByPath = function (views: WorkView[], path: string): WorkView {
  const view = <WorkView>_.find(views, view => {
    // console.log(view);
    return view.pathRegExp.test(path);
  });
  if (view) {
    const immutableView = Immutable.from({
      id: (view as WorkView).id,
      path: view.path,
      pathParam: view.pathParam ? view.pathParam : view.path,
      name: view.name,
      text: view.text,
      component: view.component
    });
    return immutableView as WorkView;
  } else {
    return null;
  }
};

export const getWorkViewByPath = function (path: string): WorkView {
  return getViewByPath(workViews, path);
};

export const getParamFromHash = function (path: string, paramPath: string) {
  let result = null;
  if (!path || !paramPath) {
    return result;
  }
  const reg = /[?]*[#]?/g;
  path = path.replace(reg, '');
  paramPath = paramPath.replace(reg, '');

  const pathArray = path.split('/');
  const paramArray = paramPath.split('/');
  const isParam = /^[$][{][\s\S]{1,100}[}]$/;
  const paramKeyLeft = /[$][{]/g;
  const paramKeyRight = /[}]/g;
  const size = pathArray.length > paramArray.length ? paramArray.length : pathArray.length;
  for (let i = 0; i < size; i++) {
    let item = paramArray[i];
    if (isParam.test(item)) {
      result = result || {};
      let key = item.replace(paramKeyLeft, '').replace(paramKeyRight, '');
      let val = pathArray[i];
      result[key] = val;
    }
  }
  return result;
};

export const paramToHash = function (paramPath: string, param: any): string {
  let result: string = paramPath;
  if (!paramPath || !param) {
    return result;
  }
  const pathArray = paramPath.split('/');
  const isParam = /^[$][{][\s\S]{1,100}[}]$/;
  const paramKeyLeft = /[$][{]/g;
  const paramKeyRight = /[}]/g;
  const size = pathArray.length;
  for (let i = 0; i < size; i++) {
    let item = pathArray[i];
    if (isParam.test(item)) {
      let key = item.replace(paramKeyLeft, '').replace(paramKeyRight, '');
      let val = param[key];
      pathArray[i] = val;
    }
  }
  result = pathArray.join('/');
  return result;
};
